﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace Super.Collection.Core
{
    public class HttpLoader
    {
        /// <summary>
        /// Http请求单例
        /// </summary>
        public static readonly HttpLoader Instance;

        /// <summary>
        /// 静态构造函数
        /// </summary>
        static HttpLoader()
        {
            Instance = new HttpLoader();
        }

        /// <summary>
        /// Http请求处理
        /// </summary>
        private HttpWebRequest request;

        /// <summary>
        /// Http请求处理
        /// </summary>
        public HttpWebRequest Request
        {
            get { return request; }
        }

        /// <summary>
        /// Http响应处理
        /// </summary>
        private HttpWebResponse response;

        /// <summary>
        /// Http响应处理
        /// </summary>
        public HttpWebResponse Response
        {
            get { return response; }
        }

        /// <summary>
        /// Http载入器的构造
        /// </summary>
        public HttpLoader()
        {
            //空实现
        }

        /// <summary>
        /// Http载入器的构造
        /// </summary>
        /// <param name="uri">指定的uri</param>
        public HttpLoader(Uri uri)
        {
            this.request = (HttpWebRequest)WebRequest.Create(uri);
        }

        /// <summary>
        /// Http载入器的构造
        /// </summary>
        /// <param name="url">指定的url</param>
        public HttpLoader(string url)
        {
            this.request = (HttpWebRequest)WebRequest.Create(url);
        }

        /// <summary>
        /// Get请求
        /// </summary>
        private void GetRequest()
        {
            this.request.Method = "GET";
            this.response = (HttpWebResponse)this.request.GetResponse();
        }

        /// <summary>
        /// Get请求
        /// </summary>
        private void GetRequest(Uri uri)
        {
            this.request = (HttpWebRequest)WebRequest.Create(uri);
            this.GetRequest();
        }

        private void GetRequest(string url)
        {
            this.GetRequest(new Uri(url));
        }

        /// <summary>
        /// Post请求
        /// </summary>
        private void PostRequest(string url, string parameters, Encoding encoding)
        {
            byte[] byteArray = encoding.GetBytes(parameters);
            this.request.Method = "POST";
            this.request = (HttpWebRequest)WebRequest.Create(url);
            this.request.ContentType = "application/x-www-form-urlencoded";
            this.request.ContentLength = byteArray.Length;
            Stream newStream = this.request.GetRequestStream();
            newStream.Write(byteArray, 0, byteArray.Length);
            newStream.Close();
            this.response = (HttpWebResponse)this.request.GetResponse();
        }

        /// <summary>
        /// 保存响应流至内存中
        /// </summary>
        /// <returns>响应的内存流</returns>
        public MemoryStream SaveResponseToMemory()
        {
            //开始请求
            this.GetRequest();
            return this.StuffToMemoryStream();
        }

        /// <summary>
        /// 填充至内存流
        /// </summary>
        /// <returns>响应的内存流</returns>
        private MemoryStream StuffToMemoryStream()
        {
            //每次从网络流中读取1024个字节至内存流
            byte[] buffer = new byte[1024];
            MemoryStream ms = new MemoryStream();
            Stream netStream = this.response.GetResponseStream();
            int count = netStream.Read(buffer, 0, buffer.Length);
            while (count > 0)
            {
                ms.Write(buffer, 0, count);
                count = netStream.Read(buffer, 0, buffer.Length);
            }
            ms.Flush();
            ms.Seek(0, SeekOrigin.Begin);
            netStream.Close();
            this.response.Close();
            return ms;
        }

        /// <summary>
        /// 保存响应流至内存中
        /// </summary>
        /// <param name="uri">请求的URI</param>
        /// <returns>响应的内存流</returns>
        public MemoryStream SaveResponseToMemory(Uri uri)
        {
            this.GetRequest(uri);
            return this.StuffToMemoryStream();
        }

        /// <summary>
        /// 保存响应流至内存中
        /// </summary>
        /// <param name="url">请求的url</param>
        /// <returns>响应的内存流</returns>
        public MemoryStream SaveResponseToMemory(string url)
        {
            this.GetRequest(url);
            return this.StuffToMemoryStream();
        }

        /// <summary>
        /// 保存响应流至内存中
        /// </summary>
        /// <param name="url">请求的url</param>
        /// <returns>响应的内存流</returns>
        public MemoryStream SaveResponseToMemory(string url, string parameters, Encoding encoding)
        {
            this.PostRequest(url, parameters, encoding);
            return this.StuffToMemoryStream();
        }
    }
}
